//
//  WBWelcomeView.swift
//  SinaWebo
//
//  Created by 亚杏 on 2017/5/22.
//  Copyright © 2017年 oms. All rights reserved.
//

import UIKit
import SDWebImage

//欢迎视图
class WBWelcomeView: UIView {

    @IBOutlet weak var avatarImageV: UIImageView!
    @IBOutlet weak var welLabel: UILabel!
    @IBOutlet weak var bottomCons: NSLayoutConstraint!//底部约束


    class func welcomeView() -> WBWelcomeView {
        let nib = UINib(nibName: "WBWelcomeView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        //从XIB加载视图是默认为600*600的
        v.frame = UIScreen.main.bounds
        return v

    }

    //视图被添加到window上，表示视图已经显示
    override func didMoveToWindow() {
        super.didMoveToWindow()

        //layoutIfNeeded 会直接按照当前约束直接更新控件位置，就是xib中位置
        self.layoutIfNeeded()
        //- 当视图被添加到窗口上时，根据父视图的大小，计算约束值，更新控件位置
        bottomCons.constant = bounds.size.height - 200

        //damping：弹力系数, initialVelocity:初始速度
        UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            //更新约束
            self.layoutIfNeeded()

        }) { (_) in
            UIView.animate(withDuration: 1.0, animations: { 
                self.welLabel.alpha = 1
            }, completion: { (_) in
                self.removeFromSuperview()
            })
        }
        }

    //此方法只是刚刚从xib的二进制文件将视图数据加载完成，还没有和代码连线建立关系，开发时不要在此方法中处理UI
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        //设置图像
        guard let urlStr = WBNetworkManager.shared.userAccount.avatar_large,
            let url = URL(string: urlStr)
            else{
            return
        }

        avatarImageV.sd_setImage(with: url, placeholderImage: UIImage(named: "avatar_default_big"))
        avatarImageV.layer.cornerRadius = 42.5
        avatarImageV.layer.masksToBounds = true
    }


}
